﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using System.Data.SqlClient;
using DbCommon;
using System.Data;

namespace DAL
{
    public class PrivilegesDAL
    {
        DbHelper dbhelper = new DbHelper();
        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns></returns>
        public int Insert(BoPrivileges model)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();
            sql.Append("INSERT INTO [Privileges]");
            sql.Append("([PRI_ROLES],[PRI_MODULES])");
            sql.Append("VALUES");
            sql.Append("(@PRI_ROLES,@PRI_MODULES)");
            return dbhelper.ExcuteNoQuery(sql.ToString(), GetParameters(model));
        }

        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns></returns>
        public int Update(BoPrivileges model)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();
            sql.Append("UPDATE [Privileges] SET ");
            sql.Append(" WHERE ");
            sql.Append("PRI_ROLES=@PRI_ROLES"); sql.Append("PRI_MODULES=@PRI_MODULES");

            return dbhelper.ExcuteNoQuery(sql.ToString(), GetParameters(model));
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <returns></returns>
        public int Delete(string pRI_ROLES, string pRI_MODULES)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();
            sql.Append("DELETE FROM Privileges");
            sql.Append(" WHERE ");
            sql.Append("PRI_ROLES=@PRI_ROLES AND PRI_MODULES=@PRI_MODULES");
            SqlParameter[] parameter = new SqlParameter[2];
            parameter[0] = new SqlParameter("@PRI_ROLES", SqlDbType.NVarChar, 50);
            parameter[1] = new SqlParameter("@PRI_MODULES", SqlDbType.NVarChar, 50);
            parameter[0].Value = pRI_ROLES;
            parameter[1].Value = pRI_MODULES;
            return dbhelper.ExcuteNoQuery(sql.ToString(), parameter);
        }

        public int ClearPrivileges(string roleId)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();
            sql.Append("DELETE FROM Privileges ");
            sql.Append("WHERE PRI_ROLES=@PRI_ROLES");
            SqlParameter[] parameter = new SqlParameter[1];
            parameter[0] = new SqlParameter("@PRI_ROLES", SqlDbType.NVarChar, 50);
            parameter[0].Value = roleId;
            return dbhelper.ExcuteNoQuery(sql.ToString(), parameter);
        }

        /// <summary>
        /// 获取一条记录
        /// </summary>
        /// <returns></returns>
        public int GetModel(string pRI_ROLES, string pRI_MODULES)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();
            sql.Append("SELECT PRI_ROLES,PRI_MODULES FROM Privileges");
            sql.Append(" WHERE ");
            sql.Append("PRI_ROLES=@PRI_ROLES AND PRI_MODULES=@PRI_MODULES");
            SqlParameter[] parameter = new SqlParameter[2];
            parameter[0] = new SqlParameter("@PRI_ROLES", SqlDbType.NVarChar, 50);
            parameter[1] = new SqlParameter("@PRI_MODULES", SqlDbType.NVarChar, 50);
            parameter[0].Value = pRI_ROLES;
            parameter[1].Value = pRI_MODULES;
            DataTable table = dbhelper.ExcuteQuery(sql, parameter);
            IList<BoPrivileges> list = GetList(table);
            return list != null && list.Count > 0 ? list.Count:0;
        }

        private IList<BoPrivileges> GetList(DataTable table)
        {
            List<BoPrivileges> list = new List<BoPrivileges>();
            if (table != null && table.Rows.Count > 0)
            {
                BoPrivileges model = new BoPrivileges();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    model.PRI_ROLES = (string)table.Rows[i]["PRI_ROLES"];
                    model.PRI_MODULES = (string)table.Rows[i]["PRI_MODULES"];
                    list.Add(model);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns></returns>
        private SqlParameter[] GetParameters(BoPrivileges model)
        {
            SqlParameter[] parameters = new SqlParameter[] {
                new SqlParameter("@PRI_ROLES",SqlDbType.NVarChar,50),
                new SqlParameter("@PRI_MODULES",SqlDbType.NVarChar,50),
            };
            parameters[0].Value = model.PRI_ROLES;
            parameters[1].Value = model.PRI_MODULES;
            return parameters;
        }
    }
}